
// MFC_ZPL_DemoDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "MFC_ZPL_Demo.h"
#include "MFC_ZPL_DemoDlg.h"
#include "afxdialogex.h"
#include "printer_base.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


#define STATE_HIGHT_TEMP 0x00000001				 //高温
#define STATE_STANDBY    0x00000002				 //待机	
#define STATE_PRINTING   0x00000004				 //打印中
#define STATE_TOF_ERROR  0x00000008				 //标签定位异常
#define STATE_LABEL_END  0x00000010				 //纸用尽
#define STATE_RIBBON_END 0x00000020				 //碳带用尽
#define STATE_LABEL_SEIZING 0x00000040           //卡纸
#define STATE_LABEL_JUMPPING 0x00000080			 //跳标
#define STATE_LABEL_CALIBRATING 0x00000100		 //标签学习中
#define STATE_CUTTER_ERROR 0x00000200			 //切刀异常
#define STATE_FORM_ERROR 0x00000400				 //标签格式异常
#define STATE_MEMORY_WRITE_ERROR 0x00000800		 //记忆体写入异常
#define STATE_ILLEGAL_COMMAND 0x00001000		 //非法指令
#define STATE_COVER_OPEN 0x00002000				 //盒盖未到位
#define STATE_RIBBON_NEAR_END 0x00004000		 //碳带将尽


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

	// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

	// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CMFC_ZPL_DemoDlg 对话框



CMFC_ZPL_DemoDlg::CMFC_ZPL_DemoDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMFC_ZPL_DemoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFC_ZPL_DemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMFC_ZPL_DemoDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(btntest, &CMFC_ZPL_DemoDlg::OnBnClickedbtntest)
	ON_BN_CLICKED(IDC_USBLIST, &CMFC_ZPL_DemoDlg::OnBnClickedUsblist)
	ON_BN_CLICKED(IDC_FILE, &CMFC_ZPL_DemoDlg::OnBnClickedFile)
	ON_EN_CHANGE(IDC_FILENAME, &CMFC_ZPL_DemoDlg::OnEnChangeFilename)
	ON_BN_CLICKED(IDC_FIRMWARE, &CMFC_ZPL_DemoDlg::OnBnClickedFirmware)
END_MESSAGE_MAP()


// CMFC_ZPL_DemoDlg 消息处理程序

BOOL CMFC_ZPL_DemoDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	bool b = NULL == nullptr;

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO:  在此添加额外的初始化代码

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CMFC_ZPL_DemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CMFC_ZPL_DemoDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMFC_ZPL_DemoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

#include <Windows.h>
#include <atlimage.h>
#include <string>


void OnBnClickedHl()
{
	// TODO: 在此添加控件通知处理程序代码
	void* pHandle = NULL;
	int iResult = -1;
	CString strModelSetting;
	char *pModel = (char *)strModelSetting.GetBuffer(strModelSetting.GetAllocLength());
	if (0 == (iResult = fPrinterCreator(&pHandle, "HT300")))
	{
		iResult = fPortOpen(pHandle, "USB");
		if (iResult == 0)
		{

			iResult = fStartFormat(pHandle);

			fRfidReturnHostDatalog(pHandle);

			//结束
			iResult = fEndFormat(pHandle);

			//删除打印机
			iResult = fPortClose(pHandle);

			//销毁
			iResult = fPrinterDestroy(pHandle);
		}
		else
		{
			AfxMessageBox(_T("Usb Device not found."));
			return;
		}
	}
	else
	{
		AfxMessageBox(_T("Printer Device not found."));
		return;
	}
}


void OnBnClickedHr()
{
	// TODO: 在此添加控件通知处理程序代码
	void* pHandle = NULL;
	int iResult = -1;
	CString strModelSetting;
	char* pStartStr = "start";
	char* pEndStr = "end";
	char* pStartPosition = "B20";
	char* pEndPosition = "F42";
	char model = 'M';

	char *pModel = (char *)strModelSetting.GetBuffer(strModelSetting.GetAllocLength());
	if (0 == (iResult = fPrinterCreator(&pHandle, "HT300")))
	{
		iResult = fPortOpen(pHandle, "USB");
		if (iResult == 0)
		{
			iResult = fStartFormat(pHandle);

			fRfidCorrectXpdnrPosition(pHandle, pStartStr, pEndStr, pStartPosition, pEndPosition, model);

			//结束
			iResult = fEndFormat(pHandle);

			//删除打印机
			iResult = fPortClose(pHandle);

			//销毁
			iResult = fPrinterDestroy(pHandle);
		}
		else
		{
			AfxMessageBox(_T("Usb Device not found."));
			return;
		}
	}
	else
	{
		AfxMessageBox(_T("Printer Device not found."));
		return;
	}
}


void OnBnClickedRb()
{
	// TODO: 在此添加控件通知处理程序代码
	void* pHandle = NULL;
	int iResult = -1;
	CString strModelSetting;
	int nTotalNum = 96;
	int pPartitionSize[] = { 8,3,3,20,24,38 };
	int nPartitionLenth = sizeof(pPartitionSize) / sizeof(pPartitionSize[0]);
	char *pModel = (char *)strModelSetting.GetBuffer(strModelSetting.GetAllocLength());
	if (0 == (iResult = fPrinterCreator(&pHandle, "HT300")))
	{
		iResult = fPortOpen(pHandle, "USB");
		if (iResult == 0)
		{
			//写
			iResult = fStartFormat(pHandle);

			iResult = fRfidDefineDataStruct(pHandle, nTotalNum, pPartitionSize, nPartitionLenth);

			fRfidWrite(pHandle, 'E', 0, 1, 'E', _T("48,1,6,770289,10001025,1"));

			//结束
			iResult = fEndFormat(pHandle);

			//删除打印机
			iResult = fPortClose(pHandle);

			//销毁
			iResult = fPrinterDestroy(pHandle);
		}
		else
		{
			AfxMessageBox(_T("Usb Device not found."));
			return;
		}
	}
	else
	{
		AfxMessageBox(_T("Printer Device not found."));
		return;
	}
}


void OnBnClickedRr()
{
	// TODO: 在此添加控件通知处理程序代码
	void* pHandle = NULL;
	int iResult = -1;
	CString strModelSetting;
	int nRetryCount = 2;
	char *pModel = (char *)strModelSetting.GetBuffer(strModelSetting.GetAllocLength());
	if (0 == (iResult = fPrinterCreator(&pHandle, "HT300")))
	{
		iResult = fPortOpen(pHandle, "USB");
		if (iResult == 0)
		{
			iResult = fStartFormat(pHandle);

			fRfidRetryCount(pHandle, nRetryCount);

			fRfidWrite(pHandle, 'H', 0, 1, 'E', _T("1234"));

			//结束
			iResult = fEndFormat(pHandle);

			//删除打印机
			iResult = fPortClose(pHandle);

			//销毁
			iResult = fPrinterDestroy(pHandle);
		}
		else
		{
			AfxMessageBox(_T("Usb Device not found."));
			return;
		}
	}
	else
	{
		AfxMessageBox(_T("Printer Device not found."));
		return;
	}
}


void OnBnClickedRs()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	void* pHandle = NULL;
	int iResult = -1;
	CString strModelSetting;
	int nTagNum = 2;
	int nErrorAct = 'P';
	char *pModel = (char *)strModelSetting.GetBuffer(strModelSetting.GetAllocLength());
	if (0 == (iResult = fPrinterCreator(&pHandle, "HT300")))
	{
		iResult = fPortOpen(pHandle, "USB");
		if (iResult == 0)
		{
			iResult = fStartFormat(pHandle);

			fRfidSetParameters(pHandle, nTagNum, nErrorAct);

			//结束
			iResult = fEndFormat(pHandle);

			//删除打印机
			iResult = fPortClose(pHandle);

			//销毁
			iResult = fPrinterDestroy(pHandle);
		}
		else
		{
			AfxMessageBox(_T("Usb Device not found."));
			return;
		}
	}
	else
	{
		AfxMessageBox(_T("Printer Device not found."));
		return;
	}

}


void OnBnClickedRw()
{
	// TODO: 在此添加控件通知处理程序代码
	void* pHandle = NULL;
	int iResult = -1;
	CString strModelSetting;
	int nReadPower = 'H';
	int nWritePower = 'H';
	int nAntennaType = 1;
	char *pModel = (char *)strModelSetting.GetBuffer(strModelSetting.GetAllocLength());
	if (0 == (iResult = fPrinterCreator(&pHandle, "HT300")))
	{
		iResult = fPortOpen(pHandle, "USB");
		if (iResult == 0)
		{
			//iResult = StartFormat(pHandle);

			fRfidSetPowerLevel(pHandle, nReadPower, nWritePower, nAntennaType);

			//结束
			//iResult = EndFormat(pHandle);

			//删除打印机
			iResult = fPortClose(pHandle);

			//销毁
			iResult = fPrinterDestroy(pHandle);
		}
		else
		{
			AfxMessageBox(_T("Usb Device not found."));
			return;
		}
	}
	else
	{
		AfxMessageBox(_T("Printer Device not found."));
		return;
	}
}


void OnBnClickedRz()
{
	// TODO: 在此添加控件通知处理程序代码
	void* pHandle = NULL;
	int iResult = -1;
	CString strModelSetting;
	char *password = "1234ABCD";
	int nMemoryBlock = 'E';
	char locktype = 'L';
	char *pModel = (char *)strModelSetting.GetBuffer(strModelSetting.GetAllocLength());
	if (0 == (iResult = fPrinterCreator(&pHandle, "HT300")))
	{
		iResult = fPortOpen(pHandle, "USB");
		if (iResult == 0)
		{
			iResult = fStartFormat(pHandle);

			fRfidWrite(pHandle, 'H', 0, 1, 'E', _T("112233445566778899001122"));

			fRfidSetLockTagAndPassword(pHandle, password, nMemoryBlock, locktype);

			//结束
			iResult = fEndFormat(pHandle);

			//删除打印机
			iResult = fPortClose(pHandle);

			//销毁
			iResult = fPrinterDestroy(pHandle);
		}
		else
		{
			AfxMessageBox(_T("Usb Device not found."));
			return;
		}
	}
	else
	{
		AfxMessageBox(_T("Printer Device not found."));
		return;
	}
}


void OnBnClickedRu()
{
	// TODO: 在此添加控件通知处理程序代码
	void* pHandle = NULL;
	int iResult = -1;
	CString strModelSetting;
	char *pModel = (char *)strModelSetting.GetBuffer(strModelSetting.GetAllocLength());
	if (0 == (iResult = fPrinterCreator(&pHandle, "HT300")))
	{
		iResult = fPortOpen(pHandle, "USB");
		if (iResult == 0)
		{

			iResult = fStartFormat(pHandle);

			iResult = fRfidReadChipSerialization(pHandle);

			iResult = fScalableFontText(pHandle, 10, 10, '0', 'N', 50, 50, _T("Serial Number: #H"));

			iResult = fRfidWrite(pHandle, 'H', 0, 1, 'E', _T("12#H"));

			//结束
			iResult = fEndFormat(pHandle);

			//删除打印机
			iResult = fPortClose(pHandle);

			//销毁
			iResult = fPrinterDestroy(pHandle);
		}
		else
		{
			AfxMessageBox(_T("Usb Device not found."));
			return;
		}
	}
	else
	{
		AfxMessageBox(_T("Printer Device not found."));
		return;
	}
}

void CMFC_ZPL_DemoDlg::OnBnClickedbtntest()
{
	// TODO:  在此添加控件通知处理程序代码
	void* pHandle = NULL;
	char* cUtf8Data = NULL;
	unsigned char cRfidData[8] = { 0 };
	unsigned int preadedNum = 0;
	int iResult = 0;
	int dwStateData = 0;
	int StatusData_ = 0;
	unsigned char* StatusData = (unsigned char*)&StatusData_;
	iResult = fPrinterCreator(&pHandle, "iX4P3");	//HT300
	if (iResult == 0)
	{
		//iResult = PortOpen(pHandle, "NET,192.168.1.153");
		iResult = fPortOpen(pHandle, "USB");	//Port_#0010.Hub_#0001
		if (iResult == 0)
		{
			//开始
			iResult = fStartFormat(pHandle);
			//获取版本号
			char verison[40] = { 0 };
			//iResult = fGetPrinterFirmwareVersion(pHandle, verison);
			printf("verison = %s", verison);
			//设置了打印浓度
			iResult = fSetPrintDarkness(pHandle, 20);
			//设置标签长度
			iResult = fSetLabelLength(pHandle, 1400);
			//打印文本
			iResult = fSetChangeFontEncoding(pHandle, 14);
			iResult = fPrintText(pHandle, 0, 100, 16, 0, 40, 40, "中文");

			//打印Pdf417
			iResult = fPdf417(pHandle, 0, 150, 0, 2, 20, 2, 5, 5, 'N', "0010101101");

			//打印MicroPdf417
			iResult = fMicroPdf417(pHandle, 100, 250, 0, 2, 20, 23, "0010101101");

			//打印二维码
			iResult = fQRCode(pHandle, 550, 200, 0, 1, 10, 'M', 'M', 'N', "1000000001");

			//打印BarCode39条码
			iResult = fBarCode39(pHandle, 0, 300, 0, 3, 50, 'Y', 'Y', 'Y', "66886868");

			//打印CodeEan8条码
			iResult = fCodeEan8(pHandle, 0, 400, 0, 3, 60, 'Y', 'Y', "90381010");

			//打印UPC-A
			iResult = fUpcaBarcode(pHandle, 0, 500, 0, 3, 50, 'Y', 'Y', 'N', "47631664454");

			//打印UPC扩展条码
			iResult = fUpcExtensions(pHandle, 0, 600, 0, 3, 50, 'Y', 'Y', "01234");

			//打印图片
			iResult = fPrintImage(pHandle, 0, 850, "D:\\Image\\300dpi.bmp");

			//打印图形圆圈
			iResult = fGraphicCircle(pHandle, 450, 0, 150, 5);

			//打印图形椭圆
			iResult = fGraphicEllipse(pHandle, 450, 70, 150, 100, 10);

			//打印对角线
			iResult = fGraphicDiagonaline(pHandle, 600, 140, 0x52, 50, 50, 20);

			//打印图形框
			iResult = fGraphicBox(pHandle, 450, 400, 100, 100, 60, 3);

			//打印符号
			iResult = fGraphicSymbol(pHandle, 550, 400, 180, 200, 200, "A");

			//获取状态
			iResult = fGetPrinterStatus(pHandle, &dwStateData);

			//RFID读写
			//iResult = fRfidWrite(pHandle, 'H', 2, 4, '1', _T("12345678"));
			//iResult = fRfidRead(pHandle, 'H', 2, 4, '1', "", _T(""));

			//RFID空读取
			//iResult = fRfidReadEmpty(pHandle);

			//设置是否切刀、剥离等
			iResult = fSetPrintMode(pHandle, 'C', 'Y');
			//iResult = fSetPrintQuantity(pHandle, 1, 0, 1, 'Y');
			iResult = fCutter(pHandle, 20);//0全切  10-60半切，值=介质保留未切割的毫米数
			iResult = fSetPrintQuantity(pHandle, 1, 1, 1, 'Y');

			//结束
			iResult = fEndFormat(pHandle);

			//iResult = fReadData(pHandle, cRfidData, 8, &preadedNum);

			//删除打印机
			iResult = fPortClose(pHandle);

			//销毁
			iResult = fPrinterDestroy(pHandle);
		}
		else
		{
			AfxMessageBox(_T("Usb Device not found."));
			return;
		}
	}
	else
	{
		AfxMessageBox(_T("Printer Device not found."));
		return;
	}
	//OnBnClickedHl();
	//OnBnClickedHr();
	//OnBnClickedRb();
	//OnBnClickedRr();
	//OnBnClickedRs();
	//OnBnClickedRw();
	//OnBnClickedRz();
	//OnBnClickedRu();
}


void CMFC_ZPL_DemoDlg::OnBnClickedUsblist()
{
	// TODO: 在此添加控件通知处理程序代码
	//int cnt = 0;
	//char buf[512] = { 0 };
	//DemGetUsbList(buf,512,&cnt,0);
	//printf("%s", buf);
	void* pHandle = NULL;
	int iResult = fPrinterCreator(&pHandle, "HT100");	//HT300
	if (iResult == 0)
	{
		//iResult = PortOpen(pHandle, "NET,192.168.1.153");
		iResult = fPortOpen(pHandle, "USB");	//Port_#0010.Hub_#0001
		if (iResult)
		{
			printf("openfail");
		}
		//CString strCmd = " \x1b\x1c& V1 do \"font_download_quick\"\r\n";
		//unsigned char *cmd = (unsigned char*)strCmd.GetBuffer(strCmd.GetLength());
		//fWriteData(pHandle, cmd, strCmd.GetLength());
		//unsigned char readbuf[512] = { 0 };
		//unsigned int revcount = 0;
		//fReadData(pHandle, readbuf, 512,&revcount);
		//printf("result:%s", readbuf);
	}
}

void fn(int a)
{
	printf("回到函数被使用 %d",a);
}

#include <fstream>
void CMFC_ZPL_DemoDlg::OnBnClickedFile()
{
	// TODO: 在此添加控件通知处理程序代码
	CString filename;//保存路径
	CFileDialog opendlg(TRUE, _T("*"), _T("*"), OFN_OVERWRITEPROMPT, _T("所有文件(*.*;)|*.*||"), NULL);
	if (opendlg.DoModal() == IDOK)
	{
		filename = opendlg.GetPathName();
	}
	SetDlgItemText(IDC_FILENAME, filename);
	void* pHandle = NULL;
	int iResult = fPrinterCreator(&pHandle, "HT100");	//HT300
	if (iResult == 0)
	{
		//iResult = PortOpen(pHandle, "NET,192.168.1.153");
		iResult = fPortOpen(pHandle, "USB");	//Port_#0010.Hub_#0001
		//iResult = fPortOpen(pHandle, "COM1,115200,n,8,1,n");
		char *pFileName = (char*)filename.GetBuffer(filename.GetLength());
		char dec = 0;
		int iPacketSize = 4096;
		if (iResult == 0)
		{
			fDownloadFontFile(pHandle, iPacketSize, pFileName, &dec, fn);
		}
	}
}


void CMFC_ZPL_DemoDlg::OnEnChangeFilename()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
}


void CMFC_ZPL_DemoDlg::OnBnClickedFirmware()
{
	// TODO: 在此添加控件通知处理程序代码
		// TODO: 在此添加控件通知处理程序代码
	CString filename;//保存路径
	CFileDialog opendlg(TRUE, _T("*"), _T("*"), OFN_OVERWRITEPROMPT, _T("所有文件(*.*;)|*.*||"), NULL);
	if (opendlg.DoModal() == IDOK)
	{
		filename = opendlg.GetPathName();
	}
	SetDlgItemText(IDC_EDIT2, filename);
	void* pHandle = NULL;
	int iResult = fPrinterCreator(&pHandle, "HT100");	//HT300
	if (iResult == 0)
	{
		//iResult = PortOpen(pHandle, "NET,192.168.1.153");
		iResult = fPortOpen(pHandle, "USB");	//Port_#0010.Hub_#0001
		//iResult = fPortOpen(pHandle, "COM1,115200,n,8,1,n");
		char *pFileName = (char*)filename.GetBuffer(filename.GetLength());
		char dec[] = "USB,Port_#0001.Hub_#0001";
		if (iResult == 0)
		{
			fDownloadFMWImg(pHandle, pFileName, dec, fn);
		}
	}
}